package service;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.*;
import domain.Venta;

public class SimpleVentaManager implements VentaManager {
  private List<Venta> elements;
  private List<Venta> ventas_tmp;

  public List<Venta> getAll() {return elements;}

  public void setVentas(List<Venta> elements) {this.elements = elements;}
  public List<Venta> getVentas(){return this.elements;}
  public List<Venta> getVentas_tmp(){return this.ventas_tmp;}
  public void setVentas_tmp(List<Venta> ventas_tmp){this.ventas_tmp=ventas_tmp;}
  
  public Venta getById(String id) {
    Iterator itr = elements.iterator();
    Venta element;
    while (itr.hasNext()) {
      element = (Venta)itr.next();
      if (element.getPk_venta().equals(id)) return element;
    }
    return null;
  }

  public List<Venta> getAllByDependienteID(String id){
    ventas_tmp.clear();
    Iterator itr = elements.iterator();
    Venta element;
    while (itr.hasNext()) {
      element = (Venta)itr.next();
      if (element.getFk_dependiente().equals(id))
        ventas_tmp.add(element);    
    }
    return ventas_tmp;
  }
  
  public void delete(String id){

    Iterator itr = elements.iterator();

    while (itr.hasNext()) {
      Venta element;
      element = (Venta)itr.next();
      if (element.getPk_venta().equals(id)){
        itr.remove();
  	  }
    }
  }
  
  public void insert(Venta myObj){

    if (myObj.getFecha()!=null){
      Random generator = new Random();
      int id = generator.nextInt();
	    id = Math.abs(id);
		  myObj.setPk_venta(String.valueOf(id));
		  elements.add(myObj);
    }
  }
}